/*
 * 文件名： BrandItemActivity.java
 * 描    述：品牌商品列表
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.miguo.activity.base.BaseListActivity;
import com.miguo.activity.callback.IGoods;
import com.miguo.activity.callback.IGoodsScroll;
import com.miguo.activity.callback.listener.GoodsScrollListener;
import com.miguo.adapter.GoodsAdapter;
import com.miguo.app.AppConfig;
import com.miguo.bean.CommentBean;
import com.miguo.bean.GoodsBean;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.component.BaseStatusView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;
import com.miguo.utils.CommonUtils;
import com.miguo.utils.Options;
import com.miguo.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrandItemActivity extends BaseListActivity<GoodsBean, GoodsAdapter> implements View.OnClickListener, IGoodsScroll, IGoods<GoodsBean, GoodsAdapter> {


    private int brand_id;
    private String brand_title;
    private String brand_img;
    private ImageView mHeadImage;

    private BaseHeadView mBaseHead;
    private BaseStatusView BaseStatus;
    private RelativeLayout mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand_item);
        setGoodsCallBack(this);
        initExtra();
        findViewById();
        initView();
    }

    private void initExtra() {
        Intent in = getIntent();
        Bundle bundle = in.getExtras();
        if (bundle != null) {
            brand_title = in.getStringExtra("brand_title");
            brand_img = in.getStringExtra("brand_img");
            brand_id = StringUtils.StrToInt(in.getStringExtra("brand_id"));
            Statistics.onTrackCustomClick(mContext, AppConfig.STATISTICS.EVENT_NAME_33);
        }
    }


    @Override
    protected void findViewById() {
        super.findViewById();
        mContent = (RelativeLayout) findViewById(R.id.content_view);
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setBackBtn(isBackBtn);
        mBaseHead.setActivity(this);
        BaseStatus = (BaseStatusView) findViewById(R.id.base_status_view);
        BaseStatus.setContentView(mContent);
        BaseStatus.getRefreshView().setOnClickListener(this);
        BaseStatus.getNetworkView().setOnClickListener(this);

        if (TextUtils.isEmpty(brand_title) == false) {
            mBaseHead.setTitle(brand_title);
        }
    }

    @Override
    protected void initView() {
        mHeadImage = new ImageView(this);
        mHeadImage.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        mHeadImage.setScaleType(ImageView.ScaleType.FIT_XY);
        mHeadImage.setImageResource(R.drawable.load_default);
        LayoutParams Layoutp = mHeadImage.getLayoutParams();
        Layoutp.height = CommonUtils.getHeight(mContext) / 7;
        mHeadImage.setLayoutParams(Layoutp);

        gridView.addHeaderView(mHeadImage);

        if (TextUtils.isEmpty(brand_img) == true) {
            imageLoader.displayImage(brand_img, mHeadImage, Options.getPromotionOptions());
        }
        super.initView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.refresh_rl:
            case R.id.network_error_ll:
                this.onGoodsLoadData();
                break;
        }
        super.onClick(view);
    }

    @Override
    public GoodsAdapter onGoodsLoadAdapter() {
        return new GoodsAdapter(mActivity);
    }

    @Override
    public List<GoodsBean> onGoodsDealList(JsonData data) {
        return CommentBean.GoodsData(data.optJson("item"));
    }

    @Override
    public void onGoodsLoadData() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("id", String.valueOf(brand_id));
        param.put("Page", String.valueOf(page));
        HttpClient.post(Url.toRoute(Url.Brand.Item, 1), param,
                AjaxCallBack);
    }

    @Override
    public GoodsScrollListener onGoodsScroll() {
        return new GoodsScrollListener(this);
    }

    @Override
    public void onGoodsRefresh() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFinish() {
        BaseStatus.setRefreshVisibility(View.GONE);
    }

    @Override
    public void onGoodsError() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFail() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onScrollIdle() {
        imageLoader.resume();
    }

    @Override
    public void onScrollFling() {
        if (pauseOnFling) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTouchScroll() {
        if (pauseOnScroll) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTopPositiont() {
        // 判断滚动到顶部
        if (mToTopBtn.getVisibility() == View.VISIBLE) {
            mToTopBtn.setVisibility(View.GONE);
            mToTopBtn.startAnimation(mTopDownAnima);
        }
    }

    @Override
    public void onScrollFirstLastPosition() {
        // 第一次滚动至底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
    }

    @Override
    public void onScrollLastPosition() {
        // 判断滚动到底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            this.onGoodsLoadData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }

}
